package com.leyou.item.controller;

import com.leyou.common.pojo.PageResult;
import com.leyou.item.pojo.Brand;
import com.leyou.item.service.BrandService;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    //通过page页数查询所有的品牌
    @GetMapping("page")
    public ResponseEntity<PageResult<Brand>>  queryBrandByPage(
            //请求参数到后台
            //传入参数数据 page 页数   rows 列
            //required  说明请求参数是可以有，也可以不有的，不是必须的
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "5") Integer rows,
            @RequestParam(value = "sortBy",required = false) String sortBy,
            @RequestParam(value = "desc",defaultValue = "false") Boolean desc,
            @RequestParam(value = "key",required = false)    String key){
        PageResult<Brand> pageResult = this.brandService.queryBrandByPage(page,rows,sortBy,desc,key);
        //判断pageResult里面不能为空，pageResult的内容不能为空，pageResult内容的值，也就是个数不为0
        if (pageResult != null && pageResult.getItems() != null &&  pageResult.getItems().size() != 0){
            //响应成功，返回200状态码，响应出所有的品牌数据集合
            return  ResponseEntity.ok(pageResult);
        }
          //响应失败，返回204状态码
         return  ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @PostMapping
    public ResponseEntity<Void> saveBrand(Brand brand,@RequestParam("cids")List<Long> cids){

        //springmvc将带有逗号的字符串切割为数组
        //调用保存品牌的方法，保存name和letter，保存品牌数据
        this.brandService.saveBrand(brand,cids);
        //保存成功后返回201状态码
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

}